/**
 * 用户统计模块模型
 */

//用户注册统计数
$(function(){
	init();//初始化
	$("#yearStat").change(function(){reloadUserStat(); } );
	 //整体方法初始化
	function init(){
		//初始化加载统计日期
		$.post("../system/static/queryUserYears",function(data){for(var i = 0 ; i < data.length ; i++){$("#yearStat").append("<option value ='"+data[i]+"'>"+data[i]+"</option>");}});
		//初始化加载用户月份统计信息
		reloadUserStat();
	}
	
   //用户注册用户统计设置
   var userRegisterChart = echarts.init(document.getElementById('usersRegistBar'));
	
   //用户图数据设置
   var userRegistOption = {
	        tooltip: {trigger: 'axis'},legend: {data: ['用户注册数']},
	        toolbox: {show: true,feature: {mark: {show: true},dataView: {show: true,readOnly: false},magicType: {show: true,type: ['line', 'bar']},restore: {show: true},saveAsImage: {show: true}}},
	        calculable: true,
	        xAxis: [{type: 'category',data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']}],
	        yAxis: [{type: 'value',splitArea: {show: true}}],series: [{name: '用户注册数',type: 'bar',data: []}]
   }
	
	function reloadUserStat(){
		$.post("../system/static/userReqistMonthStatic",{year:'2018'},function(data){
		   var xData = userRegistOption.xAxis[0].data;
		   var yData = [];
		   for(var i=0;i<xData.length;i++){
			   for(var j = 0;j < data.length;j++){
				   if( (i+1) == (data[j].MONTH || data[j].month)){
					  yData[i] = (data[j].AMOUNT || data[j].amount);
					  break;
				   }else{
					  yData[i] = 0;
				   }
			   }
		   }
		   userRegistOption.series[0].data = yData;
		   userRegisterChart.setOption(userRegistOption);
		});
	}
	
   //各种角色用户占比
   var roleUserChar = echarts.init(document.getElementById('userRoleStat'));
   var option = {tooltip: {trigger: 'item',formatter: "{a} <br/>{b}: {c} ({d}%)"},
                legend: {orient: 'vertical',x: 'left',data:[]},
                toolbox: {show: true,feature: {dataView: {show: true,readOnly: false},restore: {show: true},saveAsImage: {show: true}}},
                series: [{name:'用户分布',type:'pie',radius: '55%',center: ['50%', '60%'],
	                      itemStyle: {emphasis: {shadowBlur: 10,shadowOffsetX: 0,shadowColor: 'rgba(0, 0, 0, 0.5)'}}
                       }]
                };
   //MySQL name value 小写
   $.post("../power/getRoleStat",function(data){
	   var newData = [];
	   for(var i=0;i<data.length;i++){
		   var name = data[i].name || data[i].NAME;
		   var value = 0;
		   if(data[i].value != 'undefined'){
			   value = data[i].value
		   }else if(data[i].VALUE != 'undefined'){
			   value = data[i].VALUE;
		   }
		   option.legend.data.push(name);
		   var temp = {};
		   temp.name = name;
		   temp.value = value;
		   newData.push(temp);
	   }
	   option.series[0].data = newData;
	   roleUserChar.setOption(option);
   });

   var operationCount1 = echarts.init(document.getElementById('operationCount1'));
   
   optionUserOptionNums = {
			        tooltip : {
			            trigger: 'axis',
			            axisPointer: {
			                type: 'shadow',
			                label: {
			                    show: true
			                }
			            }
			        },
			        toolbox: {
			            show : true,
			            feature : {
			                mark : {show: true},
			                dataView : {show: true, readOnly: false},
			                magicType: {show: true, type: ['line', 'bar']},
			                restore : {show: true},
			                saveAsImage : {show: true}
			            }
			        },
			        calculable : true,
			        legend: {
			            data:['Growth', 'Budget 2012'],
			            itemGap: 5
			        },
			        grid: {
			            top: '12%',
			            left: '1%',
			            right: '10%',
			            containLabel: true
			        },
			        xAxis: [
			            {
			                type : 'category',
			                data : []
			            }
			        ],
			        yAxis: [
			            {
			                type : 'value',
			                name : '',
			                axisLabel: {
			                    formatter: function (a) {
			                        a = +a;
			                        return isFinite(a)
			                            ? echarts.format.addCommas(+a / 1000)
			                            : '';
			                    }
			                }
			            }
			        ],
			        dataZoom: [
			            {
			                show: true,
			                start: 94,
			                end: 100
			            },
			            {
			                type: 'inside',
			                start: 94,
			                end: 100
			            },
			            {
			                show: true,
			                yAxisIndex: 0,
			                filterMode: 'empty',
			                width: 30,
			                height: '80%',
			                showDataShadow: false,
			                left: '93%'
			            }
			        ],
			        series : [
			            {
			                name: '用户操作数量',
			                type: 'bar',
			                data: []
			            },
			        ]
			    };
   
   $.post("../system/static/queryUserCountModularOperationNums",function(data){

	  var newmap ={};
	  var newname =[];
	  var newnum =[];
	  newmap = data.map;
	   for(var i=0;i<newmap.length;i++){
		   newname.push(newmap[i].NAME || newmap[i].name);
		   newnum.push(newmap[i].NUM || newmap[i].num);
	   }
	   optionUserOptionNums.xAxis[0].data= newname;
	   optionUserOptionNums.series[0].data = newnum;	
	   operationCount1.setOption(optionUserOptionNums);
   });
});

